﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public partial class CitizenFSM 
{
    //-----------------------------------IdleState------------------------------------------
    IEnumerator IdleState()
    {


        gameObject.GetComponent<Animator>().SetInteger("iAnimID", (int)BASEUNIT_STATE.MOVE);

        do
        {
            //구현

            yield return null;

            CoverEnable();
            CloseAtkEnable();
            CommonCalculate();

        } while (gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState == BASEUNIT_STATE.IDLE);

        SetState(gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState);

    }

    //-----------------------------------MoveState------------------------------------------
    IEnumerator MoveState()
    {
        gameObject.GetComponent<Animator>().SetInteger("iAnimID", (int)BASEUNIT_STATE.MOVE);

        m_vecDest = GameObject.Find("ProtectTarget").transform.position;

        gameObject.GetComponent<AStar>().AStarStart(Calculate.GetMyTileIdx(transform.position), Calculate.GetMyTileIdx(m_vecDest));

        m_BestList = gameObject.GetComponent<AStar>().GetBestList();
        
        gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState = BASEUNIT_STATE.MOVE;

        List<GameObject> listTile = GameObject.Find("DebugTile").GetComponent<Background>().m_ListTile;

        float m_fOriSpeed = gameObject.GetComponent<UnitStatus>().m_unitData.fSpeed;

        do
        {
            yield return null;

            if (listTile[m_iCurrentTileIdx].GetComponent<Tile>().m_iOption == (int)TILE_OPTION.SLOW)
                gameObject.GetComponent<UnitStatus>().m_unitData.fSpeed = 0.05f;
            else
                gameObject.GetComponent<UnitStatus>().m_unitData.fSpeed = m_fOriSpeed;


            CloseAtkEnable();
            TraceEnable();
            ThrowEnable();
            TauntEnable();


            //Astar

            BASEUNIT_STATE CurrentStat = AStarMove();
            if (CurrentStat != BASEUNIT_STATE.MOVE)
            {
                gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState = CurrentStat;
            }

            //End  
            

            if (CommonStateChange())
                break;
            

        } while (gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState == BASEUNIT_STATE.MOVE);

        SetState(gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState);
    }

    IEnumerator DamageState()
    {
        float fCurrentTime = 0.0f;

        gameObject.GetComponent<Animator>().SetInteger("iAnimID", (int)BASEUNIT_STATE.DAMAGE);
        //gameObject.GetComponent<PolyNavAgent>().Stop();

        do
        {
            yield return null;

            fCurrentTime += Time.deltaTime;

            if (fCurrentTime >= gameObject.GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).length)
            {
                fCurrentTime = 0.0f;
                gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState = BASEUNIT_STATE.MOVE;
                break;
            }

            if (CommonStateChange())
                break;

            CommonCalculate();


        } while (gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState == BASEUNIT_STATE.DAMAGE);

        SetState(gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState);
    }

    IEnumerator DeadState()
    {
        float fCurrentTime = 0.0f;

        gameObject.GetComponent<Animator>().SetInteger("iAnimID", (int)BASEUNIT_STATE.DEAD);
        //gameObject.GetComponent<PolyNavAgent>().Stop();

        ResourceMgr.GetInstance().CreateInGameText(gameObject, "$ 100", new Color(130/255f, 200/255f, 110/255f));
        GameObject.Find("SceneDataMgr").GetComponent<SceneDataMgr>().g_iMoney += 100;

        do
        {
            yield return null;

            fCurrentTime += Time.deltaTime;

            if (fCurrentTime >= gameObject.GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).length)
            {
                fCurrentTime = 0.0f;
                //시체로 변함
                Destroy(gameObject.GetComponent<CircleCollider2D>());
                //Destroy(gameObject.GetComponent<PolyNavAgent>());
                gameObject.GetComponent<SpriteRenderer>().sortingLayerName = "Props";

                GameSystem.GetInstance().DeleteThisUnit(gameObject.GetComponent<UnitStatus>(), false);
            }


        } while (gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState == BASEUNIT_STATE.DEAD);

    }

    IEnumerator RetreatState()
    {
        gameObject.GetComponent<Animator>().SetInteger("iAnimID", (int)BASEUNIT_STATE.RETREAT);
        m_vecDest = new Vector3(gameObject.transform.position.x, 10.0f, 0.0f);
        //gameObject.GetComponent<PolyNavAgent>().SetDestination(m_vecDest);

        do
        {
            yield return null;

            if (CommonStateChange())
                break;

        } while (gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState == BASEUNIT_STATE.RETREAT);

        SetState(gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState);
        
    }
}
